package com.boot2523.myrabbitmq.q3;

import com.rabbitmq.client.*;

import java.io.IOException;

public class ConsumerB {
    private static final String EXCHANGE_NAME = "ps_logs";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 声明一个 fanout 类型的 exchange（不需要声明队列，使用临时队列）
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        // 创建一个非持久化、独占、自动删除的临时队列
        String queueName = channel.queueDeclare().getQueue();

        // 将队列绑定到 exchange（不指定 routingKey）
        channel.queueBind(queueName, EXCHANGE_NAME, "");

        System.out.println(" [*] Consumer A waiting for messages...");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("Consumer A received: " + message);
        };

        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
    }
}
